import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_form_core/components/base_input_dialog_component.dart';

class BaseFormUtils {
  /// 弹出输入对话框
  static Future<Null> showCommonInputDialog(BuildContext context, {String title, Function(String value) onInputOKPress, TextInputType keyboardType = TextInputType.text, bool isMultipleLines = false, bool required, String initValue}) {
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return BaseInputDialogComponent(
          isMultipleLines: isMultipleLines,
          title: title,
          keyboardType: keyboardType,
          onInputOKPress: onInputOKPress,
          isRequired: required,
          initValue: initValue,
        );
      },
    );
  }

  /// 检查form表单中的数据 是否已经全部填写完成
  static bool checkFormData(List<BaseHttpRequestModel> requests, Map<String, dynamic> dataSource) {
    List<BaseHttpRequestModel> requiredRequests = requests.where((element) => element.required == true).toList();
    List<BaseHttpRequestModel> requiredEmptyRequests = requiredRequests.where((element) => BaseUtils.getKeyData(dataSource, element.fieldName).toString().length == 0).toList();
    if (requiredEmptyRequests.length > 0) {
      BaseUtils.showToast(requiredEmptyRequests[0].notes);
      return false;
    }
    return true;
  }
}
